﻿namespace Raven
{
    public class GoalHuntTarget : GoalComposite<RavenBot>
    {
        //this value is set to true if the last visible position of the target
        //bot has been searched without success
        private bool  _lVPTried;
        
        public GoalHuntTarget(RavenBot owner) : base(owner, GoalType.goal_hunt_target)
        {
            _lVPTried = false;
        }

        public override void Activate()
        {
            _status = GoalStatus.active;
  
            //if this goal is reactivated then there may be some existing subgoals that
            //must be removed
            RemoveAllSubgoals();
  
            //it is possible for the target to die whilst this goal is active so we
            //must test to make sure the bot always has an active target
            if (_owner.GetTargetSys().isTargetPresent())
            {
                //grab a local copy of the last recorded position (LRP) of the target
                Vector2 lrp = _owner.GetTargetSys().GetLastRecordedPosition();

                //if the bot has reached the LRP and it still hasn't found the target
                //it starts to search by using the explore goal to move to random
                //map locations
                if (lrp == Vector2.zero || _owner.isAtPosition(lrp))
                {
                    AddSubgoal(new GoalExplore(_owner));
                }

                //else move to the LRP
                else
                {
                    AddSubgoal(new GoalMoveToPosition(_owner, lrp));
                }
            }

            //if their is no active target then this goal can be removed from the queue
            else
            {
                _status = GoalStatus.completed;
            }
        }

        public override GoalStatus Process()
        {
            //if status is inactive, call Activate()
            ActivateIfInactive();

            _status = ProcessSubgoals();

            //if target is in view this goal is satisfied
            if (_owner.GetTargetSys().isTargetWithinFOV())
            {
                _status = GoalStatus.completed;
            }

            return _status;
        }

        public override void Terminate()
        {
            
        }
    }
}